package com.uiotsoft.small.sampler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.uiotsoft.util.HttpUtil;
import com.uiotsoft.util.decrypt.DecryptResponse;
import com.uiotsoft.util.encrypt.AesOld;
import com.uiotsoft.util.encrypt.Md5;
import com.uiotsoft.util.encrypt.SignCore;
import com.uiotsoft.util.encrypt.small.Aes;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

@Data
public class ComHttpClient {
    private String appKey;
    private String appSecret;
    private String gateway;
    private String token;
    private String method;
    private String data;
    private static final Logger logger = LoggerFactory.getLogger(ComHttpClient.class);

    public ComHttpClient(String appKey, String appSecret, String gateway) {
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.gateway = gateway;
    }

    private Map<String, String> getHeader(String data, String timestamp) {
        Map<String, String> headerMap = new HashMap();
        if (token != null && !token.equals("")) {
            headerMap.put("token", token);
        }
        headerMap.put("appkey", this.appKey);
        headerMap.put("method", method);
        headerMap.put("timestamp", timestamp);
        headerMap.put("nonce", "11111");
        headerMap.put("signType", "md5");
        headerMap.put("encryptType", "AES");
        headerMap.put("version", "1.0");
        headerMap.put("isEncrypt", "true");
        headerMap.put("data", data);
        String sign = Md5.sign(SignCore.createLinkString(headerMap), this.appSecret, "");
        headerMap.remove("data");
        headerMap.put("sign", sign);
        headerMap.put("Content-Type", "application/json");
        return headerMap;
    }

    public String requestPost(String unEncryptData,String time) {
        logger.debug(method + "接口的发送数据为：" + unEncryptData);
        String encryptData = AesOld.encryptAes(unEncryptData, this.appSecret);
        logger.debug("加密后的数据为：" + encryptData);
        Map<String, String> headerMap = this.getHeader(encryptData,time);
        logger.debug("请求头为：" + headerMap.toString());
        String result = HttpUtil.requestPost(this.gateway, headerMap, encryptData);
        logger.debug("返回结果未解密为：" + result);
        String resultDecrypt = DecryptResponse.parseResponse(result, this.appSecret);
        logger.debug(method + "接口的返回结果为：" + resultDecrypt);
        return resultDecrypt;
    }

    public String getHeaders(String unEncryptData,String time){
        logger.debug(method + "接口的发送数据为：" + unEncryptData);
        String encryptData = AesOld.encryptAes(unEncryptData, this.appSecret);
        logger.debug("加密后的数据为：" + encryptData);
        Map<String, String> headerMap = this.getHeader(encryptData,time);
        return headerMap.toString();
    }

    private Map<String ,String> getCommonMap(String method,String data){
        Map<String, String> allMap = new HashMap<>();
        Long timestamp = System.currentTimeMillis();
        if (token != null){
            allMap.put("token", token);
        }
        allMap.put("appkey", appKey);
        allMap.put("method", method);
        allMap.put("timestamp", timestamp+"");
        allMap.put("nonce", "11111");
        allMap.put("signType", "md5");
        allMap.put("encryptType", "AES");
        allMap.put("version", "1.0");
        allMap.put("isEncrypt", "true");
        if (data != null){
            allMap.put("data", data);
        }
        String sign = Md5.sign(SignCore.createLinkString(allMap),appSecret, "");
        allMap.put("sign",sign);
        return allMap;
    }

    public String requestGet(String unEncryptData){
        String gatewayNew;
        String encryptData = null;
        if (unEncryptData != null){
            logger.debug("接口{}的发送数据为：{}",method,unEncryptData);
            JSONObject dataJson = JSONUtil.parseObj(unEncryptData);
            Map<String,String> map = JSONUtil.toBean(dataJson,Map.class);
            encryptData = Aes.encryptAes(SignCore.createLinkString(map),appSecret);
            logger.debug("加密后的数据为：{}",encryptData);
            Map<String, String> allMap = getCommonMap(method,encryptData);
            gatewayNew = gateway +"?"+ HttpUtil.createHttpString(allMap);
        }else {
            gatewayNew = gateway;
        }
        String result = HttpUtil.requestGet(gatewayNew);
        logger.debug("返回结果未解密：{}",result);
        String resultDecrypt = DecryptResponse.parseResponse(result,appSecret);
        logger.debug("{} 返回结果解密后为：{}",method,resultDecrypt);
        return resultDecrypt;
    }
}
